<template>
  <div class="login-container">
    <div class="login-panel">
      <!-- 头像区域 -->
      <div class="avatar">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 登陆表单 -->
      <el-form ref="loginFormRef" class="login-form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="form.password" type="password"></el-input>
        </el-form-item>
        <div class="btns">
          <el-button type="primary" @click="login">登陆</el-button>
      <el-button type="info" @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginPage',
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        // 此处如何实现符合格式之后对服务器数据库已存用户的校验？？
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' },
          { max: 10, min: 3, message: '用户名长度为3-10个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' },
          { max: 16, min: 6, message: '用户名长度为6-16个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      // 如果有多于表单的其他数据  则必须手动赋初始值清除
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      // 手动调用验证
      // 为什么方括号形式不行
      // this.$refs[loginFormRef] 标红
      this.$refs.loginFormRef.validate(async valid => {
        // Eslint 导致 return 有误  需写 return false
        if (!valid) return false
        // (url, 参数)
        const { data } = await this.$http.post('login', this.form)
        // console.log(data)
        if (data.meta.status !== 200) return this.$message.error('登陆失败！')
        this.$message.success('登陆成功！')
        // 登陆成功之后要保存 token：只应在网页打开期间生效，因此放在 sessionStorage 中
        window.sessionStorage.setItem('token', data.data.token)
        // 执行跳转
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
  .login-panel {
    background-color: #fff;
    width: 480px;
    height: 320px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    .avatar {
      width: 100px;
      height: 100px;
      border: 1px solid #eee;
      border-radius: 50%;
      // overflow: hidden;
      padding: 8px;
      background-color: #fff;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%,-50%);
      img {
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
  .login-form {
      position: absolute;
      bottom: 0;
      padding: 20px;
      width: 100%;
      box-sizing: border-box;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
}
</style>
